package com.liuya.safe.policy.handler;

import com.liuya.db.script.UserCategory;
import com.liuya.db.sql.xml.DefineVariable;
import com.liuya.db.sql.xml.ExprGroup;
import com.liuya.entitle.ScriptTestResult;
import com.liuya.safe.model.SafeUser;
import com.liuya.db.script.AbstractPolicy;

import com.liuya.db.sql.xml.UserCategoryType;
import com.liuya.safe.policy.service.UserCategoryManagerService;
import com.liuya.db.script.ScriptFactory;
import com.liuya.safe.util.Factory;

import java.io.Serializable;
import java.util.Map;

//@Component("userCategoryPolicyDesignHandler")
public class UserCategoryPolicyDesignHandler extends AbstractPolicyDesignHandler implements Serializable{
//    @Autowired
//    @Qualifier("userCategoryManagerService")
//    private UserCategoryManagerService manager;
    UserCategoryType xmlUc;

    public UserCategoryPolicyDesignHandler() {
    }

//    public UserCategoryPolicyDesignHandler(UserCategoryManagerService manager, UserCategoryType xmlUc) {
    public UserCategoryPolicyDesignHandler(UserCategoryType xmlUc) {
//        this.manager = manager;
        this.xmlUc = xmlUc;
    }

    public UserCategoryType getXml() {
        return this.xmlUc;
    }

    public void setXml(UserCategoryType xml) {
        this.xmlUc = xml;
    }

    public String getPolicyType() {
        return "userCategory";
    }

    public String getManagePage() {
        return "userCategoryMng/openTree.do";
    }

    public String getDesignPageTitle() {
        return "Design User Category: " + xmlUc.getName();
    }

    public String getRawPageTitle() {
        return "Edit User Category manually: " + xmlUc.getName();
    }

    public DefineVariable[] getVariables() {
        return xmlUc.getDefineVariable();
    }

    public void deleteVariable(int id) {
        xmlUc.removeDefineVariableAt(id);
    }

    public void addVariable(DefineVariable var) {
        xmlUc.addDefineVariable(var);
    }

    public void updateVariable(int varIndex, DefineVariable var) {
        xmlUc.setDefineVariable(varIndex, var);
    }

    public ExprGroup getExprGroup() {
        return xmlUc.getExprGroup();
    }

    public boolean save(String id) {
        try{
//            manager.updateUserCategory(id, (com.liuya.db.sql.xml.UserCategory) xmlUc);
            Factory.getUserCategoryManager("").updateUserCategory(id, (com.liuya.db.sql.xml.UserCategory) xmlUc);
        } catch(Exception e){
            e.printStackTrace();
        }
        return false;
    }

    public void setDesignMode() {
        xmlUc.setIsRawScript(false);
    }

    public void setRawMode() {
        xmlUc.setIsRawScript(true);
    }

    public void setRawScript(String script) {
        xmlUc.getRawScript().setContent(script);
    }

    public String getRawScript() {
        return xmlUc.getRawScript().getContent();
    }

    public ScriptTestResult run(SafeUser user, Object businessData, Map context) {
        AbstractPolicy policy2 = getPolicy();
        UserCategory script = (UserCategory) policy2;

        return script.test(user, context, getQueryManager());
    }

    public AbstractPolicy transferXml2Policy() {
        return ScriptFactory.getUserCategory(xmlUc, getQueryManager());
    }
}